package com.etc.controller;

import com.etc.entity.SysUser;
import com.etc.service.SysOrderService;
import com.etc.service.SysStaftService;
import com.sun.jmx.snmp.SnmpUnknownSubSystemException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;

/**
 * 员工控制器
 */
@Controller
@RequestMapping("work")
public class WorkerController {

    @Autowired
    private SysStaftService sysStaftService;

    @Autowired
    private SysOrderService sysOrderService;


    /**
     * 查询所有员工
     * @param request
     * @return
     */
    @RequestMapping(value = "work", method = RequestMethod.GET)
    public String satff(HttpServletRequest request,String page){
        request.setAttribute("orderconet",sysOrderService.findByweiOrder().size());
        request.setAttribute("satff",sysStaftService.findSysStaff(Integer.valueOf(page)));
        return "worker";

    }


    /**
     * 添加员工
     * @param out
     * @param sysUser
     * @return
     */
    @RequestMapping(value = "add", method=RequestMethod.POST)
    public void add(PrintWriter out, SysUser sysUser){
        Integer restal = sysStaftService.add(sysUser);
        if(restal == 1){
            out.print(1);
        }else {
            out.print(2);
        }
    }

    /**
     * 删除员工
     * @return
     */
    @RequestMapping(value="delete" , method=RequestMethod.GET)
    public void findSyssatff(PrintWriter out, String id, HttpSession session){
        SysUser sysUser = (SysUser) session.getAttribute("user");
        if(sysUser.getRole().equals("1")){
            Integer restle = sysStaftService.delete(id);
            if(restle == 1){
                out.print(1);
            }else {
                out.print(2);
            }
        }else {
            out.print(3);
        }

    }
}
